package com.mysteel.rabbitmq.topic;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;


public final class TopicUtil {

    public static final String EXCHANGE_NAME = "demo.topic";

    public static final  String[] ROUTING_KEYS = new String[] { "demo.info", "demo.warning",
            "demo2.info", "demo.critical" };

    private static Connection connection ;
    private static Channel channel ;
    private TopicUtil(){

    }

    /**
     *  创建channel
     * @return
     */
    public static Channel createChannel(){
        try {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost(ConnectionFactory.DEFAULT_HOST);
            factory.setUsername(ConnectionFactory.DEFAULT_USER);
            factory.setPassword(ConnectionFactory.DEFAULT_PASS);
            factory.setPort(AMQP.PROTOCOL.PORT);
            factory.setVirtualHost("/demo");
             connection = factory.newConnection();
             channel = connection.createChannel();
            return  channel;
        }catch (Exception e){
            e.printStackTrace();
        }
        return  null;

    }

    /**
     *  关闭请求
     */
    public static  void  close()  {
        if(channel != null){
            try {
                channel.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }
        if(connection != null){
            try {
                connection.close();
            }catch (Exception e){
                e.printStackTrace();
            }
        }

    }
}
